package com.example.test.advice.第二章_基本类型.建议26_提防包装类型的null值;

import java.util.ArrayList;
import java.util.List;

public class Client {

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(null);
        //System.out.println(f(list));
        System.out.println(f2(list));
    }


    public static int f(List<Integer> list){
        int count = 0;
        for(int i:list){
            count +=i;//null值自动拆箱过程null.intValue 必然空指针
        }

        return count;
    }

    //优化后
    public static int f2(List<Integer> list){
        int count = 0;
        for(Integer i:list){
            count +=(i == null?0:i);
        }

        return count;
    }
}
